using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWork.HTN;
using Random = UnityEngine.Random;

namespace NPC.Enemy
{
     // BOSS巡逻任务
    public class BossPatrol : PrimitiveTask
    {
        private readonly BossAgent agent;
        
        private Vector2 leftPatrolPosition;
        private Vector2 rightPatrolPosition;
        
        private float horizontal;
        private float vertical;
    
        // 巡逻计时
        private bool _isWaiting;                 // 是否等待
        private float _waitCounter;              // 等待计时
        private float _waitingTime = 1f;         // 等待时间
    
        private bool isCanSwitchTargetPosition = true;              // 是否可以更新新的目标位置
        private Vector3 targetPosition = Vector3.zero;

        public BossPatrol(BossAgent boss, Vector2 leftPatrol, Vector2 rightPatrol)
        {
            agent = boss;
            leftPatrolPosition = leftPatrol;
            rightPatrolPosition = rightPatrol;
        }

        public override EStatus Operator()
        {
            // Debug.Log("BOSS正在巡逻");
            // 巡逻逻辑
            agent.TakeAnimation("BossWalk");
            Patrol();
            return EStatus.Success;
        }

        public void Patrol()
        {
            // 空闲等待
            if (_isWaiting)
            {
                _waitCounter += Time.deltaTime;
                if (_waitCounter > _waitingTime)
                {
                    _isWaiting = false;
                }
            }
            else
            {
                // 随机游走
                _isWaiting = true;
                _waitingTime = Random.Range(1f, 2f);

                // 可以更新新的目标位置
                if (isCanSwitchTargetPosition)
                {
                    targetPosition = new Vector3(Random.Range(leftPatrolPosition.x, rightPatrolPosition.x), 
                        Random.Range(leftPatrolPosition.y, rightPatrolPosition.y), 0f);
                    isCanSwitchTargetPosition = false;
                }
                else
                {
                    agent.transform.position = Vector3.MoveTowards(agent.transform.position, targetPosition,
                        HTNWorld.GetWorldState<float>("BossPatrolMoveSpeed") * Time.deltaTime);
                    if(Vector3.Distance(agent.transform.position, targetPosition) < 0.1f)
                        isCanSwitchTargetPosition = true;
                    
                    // 
                    if (targetPosition.y > agent.transform.position.y)
                    {
                        vertical = 0.5f;
                    }
                    else if (targetPosition.y < agent.transform.position.y)
                    {
                        vertical = 1f;
                    }
                    else
                    {
                        vertical = 0f;
                    }

                    if (targetPosition.x >= agent.transform.position.x)
                    {
                        horizontal = 1f;
                    }
                    else
                    {
                        horizontal = -1f;
                    }
                    
                    // 设置朝向
                    agent.Face(horizontal, vertical);
                }
            }
        }
    }

    // BOSS追击任务
    public class BossChase : PrimitiveTask
    {
        private readonly BossAgent agent;

        public BossChase(BossAgent boss)
        {
            agent = boss;
        }

        public override EStatus Operator()
        {
            // Debug.Log("BOSS正在追击玩家");
            // 追击逻辑
            agent.TakeAnimation("BossWalk");
            agent.ChasePlayer();
            agent.SetFace();
            return EStatus.Success;
        }
    }

    // BOSS发射火球任务
    public class BossFireball : PrimitiveTask
    {
        private readonly BossAgent agent;
        
        private float timer = 0f;
        private float randomTime = 0f;
        private const float MIN_RANDOM_TIME = 0.5f; // 最小随机时间（秒）
        private const float MAX_RANDOM_TIME = 2f;   // 最大随机时间（秒）

        public BossFireball(BossAgent boss)
        {
            agent = boss;
        }

        public override EStatus Operator()
        {
            // Debug.Log("BOSS发射火球");
            // 发射火球逻辑
            agent.TakeAnimation("BossAttack");
            // 更新计时器
            timer += Time.deltaTime;
                
            // 如果计时器达到随机时间，触发检查
            if (timer >= randomTime)
            {
                // 重置计时器并生成新的随机时间
                timer = 0f;
                randomTime = Random.Range(MIN_RANDOM_TIME, MAX_RANDOM_TIME);
                agent.TakeFireball();
            }
            agent.SetFace();
            return EStatus.Success;
        }
    }

    // BOSS召唤史莱姆任务
    public class BossSummonSlime : PrimitiveTask
    {
        private readonly BossAgent agent;
        
        private float timer = 0f;
        private float randomTime = 0f;
        private const float MIN_RANDOM_TIME = 5f; // 最小随机时间（秒）
        private const float MAX_RANDOM_TIME = 10f; // 最大随机时间（秒）

        public BossSummonSlime(BossAgent boss)
        {
            agent = boss;
        }

        public override EStatus Operator()
        {
            // Debug.Log("BOSS召唤史莱姆");
            // 召唤史莱姆逻辑
            // 更新计时器
            timer += Time.deltaTime;
                
            // 如果计时器达到随机时间，触发检查
            if (timer >= randomTime)
            {
                // 重置计时器并生成新的随机时间
                timer = 0f;
                randomTime = Random.Range(MIN_RANDOM_TIME, MAX_RANDOM_TIME);
                
                agent.TakeSlime((int)Random.Range(3, 8));
            }
            return EStatus.Success;
        }
    }

    // BOSS暴怒攻击任务
    public class BossBerserkState : PrimitiveTask
    {
        private readonly BossAgent agent;
        
        private float timer = 0f;
        private float randomTime = 0f;
        private const float MIN_RANDOM_TIME = 5f; // 最小随机时间（秒）
        private const float MAX_RANDOM_TIME = 10f; // 最大随机时间（秒）
        private const float HEALING_CHANCE = 0.3f; // 恢复生命的概率（30%）

        public BossBerserkState(BossAgent boss)
        {
            agent = boss;
        }

        public override EStatus Operator()
        {
            // Debug.Log("BOSS进入暴怒状态，发动强力攻击");
            // 更新计时器
            timer += Time.deltaTime;
                
            // 如果计时器达到随机时间，触发检查
            if (timer >= randomTime)
            {
                // 重置计时器并生成新的随机时间
                timer = 0f;
                randomTime = Random.Range(MIN_RANDOM_TIME, MAX_RANDOM_TIME);

                // 检查是否满足恢复生命的概率
                if (Random.value <= HEALING_CHANCE)
                {
                    // 暴怒概率恢复血量
                    float maxHealth = HTNWorld.GetWorldState<float>("BossMaxHealth");
                    float currentHealth = HTNWorld.GetWorldState<float>("BossHealth");
                    // 正常恢复血量
                    float restoringMinHealth = HTNWorld.GetWorldState<float>("BossRestoringHealth");
                    // 可恢复最大血量
                    float restoringMaxHealth = maxHealth - currentHealth;
                    // 随机恢复血量
                    float restoringHealth = Random.Range(restoringMinHealth, restoringMaxHealth);
                    // 更新生命值
                    float newHealth = Mathf.Min(maxHealth, currentHealth + restoringHealth);
                    HTNWorld.UpdateState("BossHealth", newHealth);
                    
                    // 输出日志
                    Debug.Log($"BOSS在暴怒状态下恢复了生命值，当前生命值: {newHealth}/{maxHealth}");
                }
            }
            return EStatus.Success;
        }
    }
}
